package com.javasm.adminapi.Tspu.contorller;

import com.github.pagehelper.PageInfo;
import com.javasm.adminapi.common.R;
import com.javasm.adminapi.common.exception.ExceptionEnum;
import com.javasm.adminapi.common.exception.JavasmException;
import com.javasm.adminapi.module.product.entity.TCategory;
import com.javasm.adminapi.module.product.entity.TSpu;
import com.javasm.adminapi.module.product.service.TCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @className: TCategoryController
 * @description:
 * @author: cx
 * @date: 2025/10/15 16:48
 * @version: 0.1
 * @since: jdk17
 */
@RestController
@RequestMapping("/tCategory")
@Slf4j
public class TCategoryController {
    @Resource
    TCategoryService tCategoryService;
    //获取所有的类目(id name)
    @GetMapping("/list")
    public R list(){
        List<TCategory> list = tCategoryService.list();
        Map<Integer,String> tCategorymap = new HashMap<>();
        list.forEach(tCategory -> {
            tCategorymap.put(Integer.parseInt(tCategory.getId()),tCategory.getCategoryName());
        });
        return R.ok(tCategorymap);
    }


    //获取所有的类目(id name)
    @GetMapping("/list1")
    public R list1(){
        List<Map<String, Object>> result = tCategoryService.CategoryByIdlist();
        return R.ok(result);
    }

    @GetMapping("/listAll")
    public R listAll(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                     @RequestParam(required = false, defaultValue = "10") Integer pageSize){
        PageInfo<TCategory> pageInfo = tCategoryService.listAll(pageNum, pageSize);
        return R.ok(pageInfo);
    }

    @GetMapping("/listAll1")
    public R listAll1(@RequestParam(required = false, defaultValue = "1")Integer pageNum,
                      @RequestParam(required = false, defaultValue = "10") Integer pageSize){
        if (pageNum == null || pageNum < 1){
            pageNum = 1;
        }
        if (pageSize == null || pageSize < 1){
            pageSize = 10;
        }
        PageInfo<TCategory> pageInfo = tCategoryService.queryCategoryByTBarnd(pageNum, pageSize);

        return R.ok(pageInfo);
    }

//    添加类别
    @PostMapping("/add")
    public R add(@RequestBody TCategory tCategory){
        boolean save = tCategoryService.saveInfo(tCategory);
        return R.ok(save);
    }


    //修改类别
    @PostMapping("/update")
    public R update(@RequestBody TCategory tCategory){
        boolean update = tCategoryService.updateByIdinfo(tCategory);
        return R.ok(update);
    }

    //删除类别
    @PostMapping("/delete")
    public R delete(@RequestBody List<Integer> id){
        if (id.isEmpty()){
            return R.ok(false);
        }
        return R.ok(tCategoryService.removeBatchByIds(id));
    }


    //根据Name查询类别
    @GetMapping("/getByName")
    public R getByName(String categoryName){
        if (categoryName == null || categoryName.isEmpty()){
            throw new JavasmException(ExceptionEnum.Parameter_Null);
        }
        TCategory byName = tCategoryService.getByName(categoryName);
        return R.ok(byName);
    }
}
